package com.guanghua.brick.html.tag;

import java.util.Collection;
import java.util.Iterator;

import javax.servlet.jsp.JspException;
import javax.servlet.jsp.tagext.BodyTagSupport;

import com.guanghua.brick.html.IDataSource;
import com.guanghua.brick.html.WebControl;

public class CoreIteratorTag extends BodyTagSupport {
	
	//private static Log logger = LogFactory.getLog(CoreIteratorTag.class);
	
	private String id = null;
	private Collection data = null;
	private String dataSource = null;
	private String index = "index"; 
	private String msg = null;
	private boolean empty = false;

	private int start = 0;
	private int count = 0;
	
	private Iterator it = null;
	private int idx = 0;
	
	public int doStartTag() throws JspException {
		if (this.data == null && this.dataSource != null) {
			IDataSource ds = WebControl.getDataSourceParser(pageContext);
			this.data = (Collection) ds.parseDataSource(pageContext, this.dataSource, Collection.class);
		}
		
		if (this.data != null) {
			it = this.data.iterator();
			while (idx < start && it.hasNext()) {
				it.next();
				idx++;
			}
			
			if (it.hasNext()) {
				pageContext.getRequest().setAttribute(this.id, it.next());
				pageContext.getRequest().setAttribute(this.index, new Integer(idx++));
				
				return BodyTagSupport.EVAL_BODY_INCLUDE;
			} else {
				this.empty = true;
				return BodyTagSupport.SKIP_BODY;
			}
		} else {
			this.empty = true;
			return BodyTagSupport.SKIP_BODY;
		}
	}
	
	public int doAfterBody() throws JspException {
		if (count > 0 && idx-start >= count) return BodyTagSupport.SKIP_BODY;
		
		if (it.hasNext()) {
			pageContext.getRequest().setAttribute(this.id, it.next());
			pageContext.getRequest().setAttribute(this.index, new Integer(idx++));
			
			return BodyTagSupport.EVAL_BODY_AGAIN;
		} else {
			return BodyTagSupport.SKIP_BODY;
		}
	}
	
	public int doEndTag() throws JspException {
		pageContext.getRequest().removeAttribute(this.id);
		pageContext.getRequest().removeAttribute(this.index);
		
		if (this.empty) pageContext.getRequest().setAttribute(this.id, new Boolean(true));
		return BodyTagSupport.EVAL_PAGE;
	}

	public int getCount() {
		return count;
	}

	public void setCount(int count) {
		this.count = count;
	}

	public String getId() {
		return id;
	}

	public void setId(String id) {
		this.id = id;
	}

	public String getIndex() {
		return index;
	}

	public void setIndex(String index) {
		this.index = index;
	}

	public int getStart() {
		return start;
	}

	public void setStart(int start) {
		this.start = start;
	}

	public Collection getData() {
		return data;
	}

	public void setData(Collection data) {
		this.data = data;
	}

	public String getDataSource() {
		return dataSource;
	}

	public void setDataSource(String dataSource) {
		this.dataSource = dataSource;
	}

	public String getMsg() {
		return msg;
	}

	public void setMsg(String msg) {
		this.msg = msg;
	}

}
